package com.mostromas.ess.product.controller;

import com.mostromas.ess.entities.Composition;
import com.mostromas.ess.entities.ProductCategory;
import com.mostromas.ess.product.req.CompositionREQ;
import com.mostromas.ess.product.req.ProductCategoryREQ;
import com.mostromas.ess.product.service.ICompositionService;
import com.mostromas.ess.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 面料成分表-前端控制器
 */
@Api(value = "面料成分管理接口",description = "面料成分管理接口，提供面料成分的增、删、改、查")
@RestController //所有方法都会返回json字符串进行相应
@RequestMapping("/composition")
public class CompositionController {

    @Autowired
    private ICompositionService compositionService;

    @PreAuthorize(("hasAuthority('composition:search')"))
    @ApiOperation("根据成分名称或英文简写查询面料成分列表接口")
    @PostMapping("/search") //请求路径： /product/composition/search
    public Result search(@RequestBody CompositionREQ req){
        return compositionService.queryList(req);
    }

    @PreAuthorize(("hasAuthority('composition:search')"))
    @ApiOperation("查询面料成分详情接口")
    @ApiImplicitParam(name = "id",value = "成分ID",required = true)
    @GetMapping("/{id}") //请求路径：/product/composition/{id}
    public Result view(@PathVariable("id") String id){
        Composition composition = compositionService.getById(id);
        return Result.ok(composition);
    }

    @PreAuthorize(("hasAuthority('composition:edit')"))
    @ApiOperation("修改面料成分信息接口")
    @PutMapping //put方式调用接口：/product/composition
    public Result update(@RequestBody Composition composition){
        compositionService.updateById(composition);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('composition:add')"))
    @ApiOperation("新增面料成分信息接口")
    @PostMapping //post方式调用接口：/product/composition
    public Result save(@RequestBody Composition composition){
        compositionService.save(composition);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('composition:delete')"))
    @ApiOperation("删除面料成分信息接口")
    @ApiImplicitParam(name = "id",value = "成分ID",required = true)
    @DeleteMapping("/{id}") //delete方式调用接口：/product/composition/{id}
    public Result delete(@PathVariable("id") String id){
        compositionService.removeById(id);
        return Result.ok();
    }

}
